package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Mark;
import cn.wolfcode.trip.base.domain.Signin;
import cn.wolfcode.trip.base.mapper.MarkMapper;
import cn.wolfcode.trip.base.mapper.SigninMapper;
import cn.wolfcode.trip.base.service.ISigninService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Service
public class SigninServiceImpl implements ISigninService {
    @Autowired
    private SigninMapper signinMapper;
    @Autowired
    private MarkMapper markMapper;
    @Override
    public void saveOrUpdate(Signin signin) {
        Signin user=signinMapper.selectByUserId(signin.getUser().getId());
        String nowYear="";
        String nowMonth="";
        String nowDay="";
        String signinYear="";
        String signinMonth="";
        String signinDay="";
        if(signin.getUser()==null){
            signinMapper.insert(signin);
        }else{
            //处理现在电脑的时间
            SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
            Date now = new Date();
            String yearDate = date.format(now);
            String[] dates = yearDate.split("-");
            nowYear=dates[0];
            nowMonth=dates[1];
            nowDay=dates[2];
            System.out.println(nowDay);
            //处理数据库中查出的签到时间
            System.out.println(user.getTime());
            String calendarDate = date.format(user.getTime());
            //分别拿出年月日
            String[] calendarDates = calendarDate.split("-");
            signinYear=calendarDates[0];
            signinMonth=calendarDates[1];
            signinDay=calendarDates[2];
            System.out.println(signinDay);
            System.out.println(Integer.valueOf(nowDay).equals( Integer.valueOf(signinDay)+1));
            Integer count = user.getCount();
            //判断其年月份是否一致,不一致将连续签到改为1(每月清空一次连续签到),并且最后一次签到的时间需要比现在的时间晚一天
            if(Integer.valueOf(nowYear).equals( Integer.valueOf(signinYear))& Integer.valueOf(nowMonth).equals( Integer.valueOf(signinMonth))
                    & Integer.valueOf(nowDay).equals( Integer.valueOf(signinDay)+1)){
                //根据user_id修改signin
                signin.setCount(++count);
                signinMapper.updateByPrimaryKey(signin);

            }else{
                    count=1;
                    signin.setCount(count);
                    signinMapper.updateByPrimaryKey(signin);
            }
            //积分的处理
            Mark mark = markMapper.selectByUserId(signin.getUser().getId());
            System.out.println(mark.getUser().getId());
            //增加积分,连续签的一天假十分,以此类推,断了或者下个月就重新开始
            Integer markMark = mark.getMark()+count*10;
            mark.setMark(markMark);//最新的积分
            //判断积分来定义修改积分的等级
            if(markMark<500){
                mark.setLevel(1);
            }else if(markMark>=500&&markMark<5000){
                mark.setLevel(2);
            }
            else if(markMark>=5000&&markMark<100000){
                mark.setLevel(3);
            }else{
                mark.setLevel(4);
            }
            //根据user.id来更新数据
            markMapper.updateByPrimaryKey(mark);

        }

    }

    @Override
    public Signin selectByUserId(Long userId) {
        Signin signin=signinMapper.selectByUserId(userId);
        return signin;
    }
}
